// src/models/universalFlow.ts
// Use 'import type' for types
import type { VNode, Component, CSSProperties } from 'vue'; // <-- Add Vue types
import type { Node, Edge, EdgeMarkerType, EdgeUpdatable } from '@vue-flow/core'; // <-- Add Vue Flow types
import { type Position } from '@vue-flow/core';

/** Defines the original format of the data source */
// Add 'toml', 'javascript', 'typescript'
export type SourceFormat = 'xml' | 'json' | 'yaml' | 'toml' | 'javascript' | 'typescript' | 'unknown';

/**
 * Defines the general semantic meaning of a node in the context of its source.
 * Optimized for XML, JSON, YAML, and TOML by focusing on common structures.
 */
export type NodeSemanticType =
    | 'root'        // Document root
    | 'element'     // XML element
    | 'object'      // JSON object / YAML mapping / TOML table
    | 'array'       // JSON array / YAML sequence / TOML array of tables
    | 'value'       // Primitive value (string, number, boolean, null, date*)
    | 'comment'     // Comment node
    | 'programBody' // <<< ADDED for V2 Processor
    | 'functionCall'        // Represents a CallExpression
    | 'variableReference'   // Represents an Identifier (not undefined)
    | 'memberExpression'    // Represents a MemberExpression (a.b, a[b])
    | 'binaryExpression'    // Represents a BinaryExpression (a + b)
    | 'unaryExpression'     // Represents a UnaryExpression (!a, -a)
    | 'conditionalExpression' // Represents a ConditionalExpression (a ? b : c)
    | 'templateLiteral'     // Represents a TemplateLiteral (`hello ${var}`)
    | 'newExpression'       // Represents a NewExpression (new Date())
    // Add more specific expression types here if needed in the future
    | 'exportDeclaration' // <<< ADDED for ExportDefaultDeclaration
    | 'sourceSnippet'     // <<< ADDED for Plan 1: Shows original source code
    | 'evaluatedResult'   // <<< ADDED for Plan 1: Shows QuickJS result placeholder/value
    | 'loop' // for, while, do-while
    | 'conditional' // if, switch, conditional expression
    | 'importDeclaration' // import ...
    | 'module' // TSModuleDeclaration
    | 'unknown'; // Default or unclassified
// *Note: TOML has native date types, potentially stored in 'value'

/**
 * Interface for the data payload of a universal flow node.
 * Optimized for XML, JSON, YAML, and TOML, balancing core info and metadata.
 */
export interface UniversalFlowNodeData {
    /** The original XML/YAML/JSON/etc. element/object/node this flow node represents. */
    sourceElement: any; // Consider creating a more specific type based on parser output
    /** Element/Node name (e.g., tag name in XML, key in YAML/JSON). */
    name: string;
    /** Text content of the node, if any. */
    content?: string;
    /** Attributes/Properties of the node. */
    attributes: Record<string, string>;
    /** 
     * Path of the node relative to the evaluated root object (e.g., 'prop.arr[0]').
     * Null if the node doesn't correspond to a part of the evaluated result.
     */
    path: string | null;
    /** Type of the source format. */
    format: SourceFormat;
    /** Indicates if the node's children are currently visible/expanded in the graph. */
    isExpanded?: boolean;

    /**
     * The primitive value if this node represents a leaf value.
     * Should only be populated for nodes with 'value' semantic type.
     * Can hold TOML date/time types (e.g., as Date objects or ISO strings).
     * Also allows RegExp and BigInt from JavaScript.
     */
    value?: string | number | boolean | null | Date | RegExp | bigint; // <<< ADDED RegExp and bigint

    /** The general semantic type of this node based on its source structure. */
    nodeSemanticType: NodeSemanticType;

    /** List of direct child node IDs, defining the primary hierarchy. */
    childrenIds?: string[];

    /**
     * Original order of this node among its siblings.
     * Required for 'array' children (JSON/YAML/TOML).
     * Required for 'element' children (XML order matters).
     * Optional for 'object' children (JSON/YAML/TOML map order preservation).
     */
    order?: number;

    /** ADDED: Depth level of the node in the hierarchy (root is 0). */
    level?: number;

    // --- Source and Metadata ---

    /** Container for format-specific metadata. */
    metadata?: {
        /** Common comment storage. */
        comment?: string | string[];
        /** XML-specific metadata. */
        xml?: {
            namespace?: string;
            isSelfClosing?: boolean;
            processingInstruction?: { target: string; data: string };
        };
        /** YAML-specific metadata. */
        yaml?: {
            tag?: string;
            anchor?: string;
            /** Indicates if this node represents a YAML alias. */
            isAlias?: boolean;
            /** If isAlias is true, this holds the ID of the node with the corresponding anchor. */
            aliasTargetId?: string;
            // Consider adding scalar style info here if needed
        };
        /** TOML-specific metadata. */
        toml?: {
            isInlineTable?: boolean; // Differentiates [table] from { k = v }
            // Add other necessary TOML nuances here
        };
        // Add other format-specific details as needed in the future
        [key: string]: any; // Allow for other arbitrary metadata
        // --- NEW: QuickJS Specific Metadata ---
        isDynamicExpression?: boolean;
        expressionString?: string;
        evaluatedValue?: any; // To store the result after evaluation
    };
}

// --- Node and Edge interfaces remain the same ---
export interface UniversalFlowNode extends Node<UniversalFlowNodeData> {}

/**
 * Data payload for universal edges.
 */
export interface UniversalFlowEdgeData {
    /** Defines the type of relationship the edge represents. */
    relationshipType?: 'parent-child' | 'alias' | string; // 'alias' for YAML references
    // ... other potential edge metadata
}

/**
 * Represents a universal edge in the Vue Flow graph.
 * Manually defining properties instead of extending Edge due to potential type conflicts.
 */
export interface UniversalFlowEdge { // No longer extends Edge<UniversalFlowEdgeData>
    id: string;
    source: string;
    target: string;
    sourceHandle?: string | null;
    targetHandle?: string | null;
    type?: string;
    label?: string | VNode | Component; // --> Uses VNode, Component
    labelStyle?: CSSProperties; // --> Uses CSSProperties
    labelShowBg?: boolean;
    labelBgStyle?: CSSProperties; // --> Uses CSSProperties
    labelBgPadding?: [number, number];
    labelBgBorderRadius?: number;
    style?: CSSProperties; // --> Uses CSSProperties
    animated?: boolean;
    hidden?: boolean;
    deletable?: boolean;
    data?: UniversalFlowEdgeData; // Keep our custom data payload
    className?: string;
    sourceNode?: UniversalFlowNode; // Assuming Node type is UniversalFlowNode
    targetNode?: UniversalFlowNode; // Assuming Node type is UniversalFlowNode
    selected?: boolean;
    markerStart?: EdgeMarkerType; // --> Uses EdgeMarkerType
    markerEnd?: EdgeMarkerType; // --> Uses EdgeMarkerType
    zIndex?: number;
    ariaLabel?: string;
    interactionWidth?: number;
    focusable?: boolean;
    updatable?: EdgeUpdatable | boolean; // --> Uses EdgeUpdatable
}

/**
 * Node metadata for additional properties.
 */
export interface UniversalFlowNodeMetadata {
  lineNumber?: number; // Start line number in the original source
  columnNumber?: number; // Start column number in the original source
  parser?: string; // Identifier for the parser that generated this node
  syntaxError?: string; // Error message if this node represents a syntax error
  sourcePath?: string; // Path of the source file, if applicable
  // NEW: Fields for QuickJS dynamic evaluation
  isDynamicExpression?: boolean; // Flag indicating if the node's value should be evaluated by QuickJS
  expressionString?: string; // The actual JS expression string to evaluate
}